// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SubtipoDAO.java

package org.fiel.admin.dao;

import java.io.PrintStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.fiel.admin.struts.forms.SubtipoForm;
import org.fiel.domain.Subtipo;

public class SubtipoDAO
{

    public SubtipoDAO()
        throws Exception
    {
        conn = null;
        try
        {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/FielDB");
            conn = ds.getConnection();
        }
        catch(Exception e)
        {
            System.err.println("SubtipoDAO() Error:\n" + e.getMessage());
            throw new Exception("SubtipoDAO() Error:\n" + e.getMessage());
        }
    }

    public void createSubtipo(SubtipoForm subtipo)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("INSERT INTO subtipos (Descripcion, DescripcionE, DesSubtipo, DesSubtipoE, Identipo) VALUES(?,?,?,?,?)");
            pstmt.setString(1, subtipo.getDescripcion());
            pstmt.setString(2, subtipo.getDescripcionE());
            pstmt.setString(3, subtipo.getDescripcionSubtipo());
            pstmt.setString(4, subtipo.getDescripcionSubtipoE());
            pstmt.setString(5, subtipo.getTipo());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("SubtipoDAO.createSubtipo() Error:\n" + e.getMessage());
            throw new Exception("SubtipoDAO.createSubtipo() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public List retrieveSubtipos()
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        List subtipos = new ArrayList();
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM subtipos ORDER BY Identipo");
            Subtipo subtipo;
            for(rset = pstmt.executeQuery(); rset.next(); subtipos.add(subtipo))
            {
                subtipo = new Subtipo();
                subtipo.setIdensubtipo(Integer.valueOf(rset.getString("Idensubtipo")));
                subtipo.setDescripcion(rset.getString("Descripcion"));
                subtipo.setDescripcionE(rset.getString("DescripcionE"));
                subtipo.setDesSubtipo(rset.getString("DesSubtipo"));
                subtipo.setDesSubtipoE(rset.getString("DesSubtipoE"));
                subtipo.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
            }

        }
        catch(Exception e)
        {
            System.err.println("SubtipoDAO.retrieveSubtiposByType() Error:\n" + e.getMessage());
            throw new Exception("SubtipoDAO.retrieveSubtiposByType() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return subtipos;
    }

    public List retrieveSubtiposByType(String tipo)
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        List subtipos = new ArrayList();
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM subtipos WHERE Identipo = ? ORDER BY Idensubtipo DESC");
            pstmt.setString(1, tipo);
            Subtipo subtipo;
            for(rset = pstmt.executeQuery(); rset.next(); subtipos.add(subtipo))
            {
                subtipo = new Subtipo();
                subtipo.setIdensubtipo(Integer.valueOf(rset.getString("Idensubtipo")));
                subtipo.setDescripcion(rset.getString("Descripcion"));
                subtipo.setDescripcionE(rset.getString("DescripcionE"));
                subtipo.setDesSubtipo(rset.getString("DesSubtipo"));
                subtipo.setDesSubtipoE(rset.getString("DesSubtipoE"));
                subtipo.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
            }

        }
        catch(Exception e)
        {
            System.err.println("SubtipoDAO.retrieveSubtiposByType() Error:\n" + e.getMessage());
            throw new Exception("SubtipoDAO.retrieveSubtiposByType() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return subtipos;
    }

    public Subtipo retrieveSubtipo(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        Subtipo subtipo = null;
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM subtipos WHERE Idensubtipo = ?");
            pstmt.setString(1, id);
            rset = pstmt.executeQuery();
            if(rset.next())
            {
                subtipo = new Subtipo();
                subtipo.setDescripcion(rset.getString("Descripcion"));
                subtipo.setDescripcionE(rset.getString("DescripcionE"));
                subtipo.setDesSubtipo(rset.getString("DesSubtipo"));
                subtipo.setDesSubtipoE(rset.getString("DesSubtipoE"));
                subtipo.setIdentipo(Integer.valueOf(rset.getString("Identipo")));
                subtipo.setIdensubtipo(Integer.valueOf(rset.getString("IdenSubtipo")));
            }
        }
        catch(Exception e)
        {
            System.err.println("SubtipoDAO.retrieveSubtipo() Error:\n" + e.getMessage());
            throw new Exception("SubtipoDAO.retrieveSubtipo() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return subtipo;
    }

    public void updateSubtipo(SubtipoForm subtipo)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("UPDATE subtipos SET Descripcion = ?, DescripcionE = ?, DesSubtipo = ?, DesSubtipoE = ?, Identipo = ? WHERE Idensubtipo = ?");
            pstmt.setString(1, subtipo.getDescripcion());
            pstmt.setString(2, subtipo.getDescripcionE());
            pstmt.setString(3, subtipo.getDescripcionSubtipo());
            pstmt.setString(4, subtipo.getDescripcionSubtipoE());
            pstmt.setString(5, subtipo.getTipo());
            pstmt.setString(6, subtipo.getId());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("SubtipoDAO.updateSubtipo() Error:\n" + e.getMessage());
            throw new Exception("SubtipoDAO.updateSubtipo() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public void deleteSubtipo(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("DELETE FROM subtipos WHERE Idensubtipo = ?");
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("SubtipoDAO.deleteSubtipo() Error:\n" + e.getMessage());
            throw new Exception("SubtipoDAO.deleteSubtipo() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    private Connection conn;
}
